package com.neouton.crm.transpondEmail.service.impl;


import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.transpondEmail.entity.TranspondEmailVO;
import com.neouton.crm.transpondEmail.mapper.TranspondEmailMapper;
import com.neouton.crm.transpondEmail.service.TranspondEmailService;
import com.neouton.feginClient.FeginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TranspondEmailServiceImpl implements TranspondEmailService {


    @Resource
    TranspondEmailMapper transpondEmailMapper;

    @Resource
    FeginService feginService;

    /**
     * 查询转发人邮箱并进行拼接
     * @param approvalId 审批ID
     */
    @Override
    public String selectTranspondEmailStr(Integer approvalId) {
        StringBuilder str = new StringBuilder();
        List<TranspondEmailVO> transpondEmailVOList = transpondEmailMapper.selectTranspondEmail(approvalId);
        if (transpondEmailVOList != null && transpondEmailVOList.size() > 0){
            // 所有用户信息集合
            List<SysUserEntity> userList = feginService.queryAllEffectiveUsers().getResult();
            for (TranspondEmailVO tp : transpondEmailVOList) {
                for (SysUserEntity su : userList) {
                    if (tp.getTranspondUseId().equals(su.getUserId())){
                        str.append(su.getEmail());
                        str.append(",");
                    }
                }
            }
            str.deleteCharAt(str.length() - 1);
        }

        return str.toString();
    }


    /**
     * 查询转发人域账号并进行拼接
     * @param approvalId 审批ID
     */
    @Override
    public String selectTranspondNameStr(Integer approvalId) {
        StringBuilder str = new StringBuilder();
        List<TranspondEmailVO> transpondEmailVOList = transpondEmailMapper.selectTranspondEmail(approvalId);
        if (transpondEmailVOList != null && transpondEmailVOList.size() > 0){
            // 所有用户信息集合
            List<SysUserEntity> userList = feginService.queryAllEffectiveUsers().getResult();
            for (TranspondEmailVO tp : transpondEmailVOList) {
                for (SysUserEntity su : userList) {
                    if (tp.getTranspondUseId().equals(su.getUserId())){
                        str.append(su.getLoginName() + " / "  + su.getAccount() );
                        str.append("，");
                    }
                }
            }
            str.deleteCharAt(str.length() - 1);
        }

        return str.toString();
    }

    /**
     * 获取抄送人员集合
     */
    @Override
    public List<SysUserEntity> queryTranspondByApprovalId(Integer approvalId){
        List<SysUserEntity> transpondUsers = new ArrayList<>();
        List<TranspondEmailVO> transpondEmailVOList = transpondEmailMapper.selectTranspondEmail(approvalId);
        if (transpondEmailVOList != null && transpondEmailVOList.size() > 0){
            // 所有用户信息集合
            List<SysUserEntity> userList = feginService.queryAllEffectiveUsers().getResult();
            for (TranspondEmailVO tp : transpondEmailVOList) {
                for (SysUserEntity su : userList) {
                    if (tp.getTranspondUseId().equals(su.getUserId())){
                        transpondUsers.add(su);
                    }
                }
            }
        }
        return transpondUsers;
    }

    /**
     * 新增转发人
     * @param transpondUsers 转发用户集合
     */
    @Override
    public void insertTranspondEmailInfoByUser(Integer approvalId,
                                               List<SysUserEntity> transpondUsers){
        if(transpondUsers!=null && transpondUsers.size()>0){
            transpondEmailMapper.insertTranspondEmailInfoByUser(approvalId,transpondUsers);
        }
    }

    /**
     * 新增转发人
     * @param transpondEmailVO
     */
    public void insertTranspondEmailInfoByType(TranspondEmailVO transpondEmailVO){
        transpondEmailMapper.insertTranspondEmailInfoByType(transpondEmailVO);
    }

    public String selectTranspondEmailStrByType(Integer typeId, Integer todoType){
        StringBuilder str = new StringBuilder();
        List<TranspondEmailVO> transpondEmailVOList = transpondEmailMapper.selectTranspondEmailByType(typeId,todoType);
        if (transpondEmailVOList != null && transpondEmailVOList.size() > 0){
            // 所有用户信息集合
            List<SysUserEntity> userList = feginService.queryAllEffectiveUsers().getResult();
            for (TranspondEmailVO tp : transpondEmailVOList) {
                for (SysUserEntity su : userList) {
                    if (tp.getTranspondUseId().equals(su.getUserId())){
                        str.append(su.getEmail());
                        str.append(",");
                    }
                }
            }
            str.deleteCharAt(str.length() - 1);
        }

        return str.toString();
    }

    public List<SysUserEntity> selectTranspondEmailByType(Integer typeId, Integer todoType){
        List<SysUserEntity> senderList = new ArrayList<>();
        List<TranspondEmailVO> transpondEmailVOList = transpondEmailMapper.selectTranspondEmailByType(typeId,todoType);
        if (transpondEmailVOList != null && transpondEmailVOList.size() > 0){
            // 所有用户信息集合
            List<SysUserEntity> userList = feginService.queryAllEffectiveUsers().getResult();
            for (TranspondEmailVO tp : transpondEmailVOList) {
                for (SysUserEntity su : userList) {
                    if (tp.getTranspondUseId().equals(su.getUserId())){
                        senderList.add(su);
                    }
                }
            }
        }
        return senderList;
    }
}